<template>
  <el-header class="index-header" style="background-color: white">
    <div class="content-container" style="width: 1170px!important;">
      <!--学生、老师页面展示学校logo-->
      <a class="login-wrapper" v-if="type == 'student' || type == 'teacher'">
        <el-image
            class="login-wrapper-image"
            style="height: 40px"
            :src="urlContentContainer"
            :fit="fit"
        ></el-image>
      </a>
      <!--管理员页面展示管理员logo-->
      <a class="login-wrapper" v-else-if="type == 'admin'">
        <el-image
            class="login-wrapper-image"
            style="height: 40px"
            :src="urlContentContainerElseIf"
            :fit="fit"
        ></el-image>
      </a>
      <!-- 主页面展示logo -->
      <a class="login-wrapper" v-else>
        <el-image
            class="login-wrapper-image"
            style="height: 40px"
            :src="urlContentContainerElse"
            :fit="fit"
        ></el-image>
      </a>

      <!--   主页标题展示-->
      <div
          class="nav-menu pull-left"
          v-if="type != 'student' && type != 'teacher' && type != 'admin'"
      >
        <router-link class="menu-item" :class="{active:shows == 1}" to=""
                     @click.native="twshow('1')">首页
        </router-link>
        <router-link class="menu-item" :class="{active:shows == 2}" to=""
                     @click.native="twshow('2')">教材
        </router-link>
        <router-link class="menu-item" :class="{active:shows == 3}" to=""
                     @click.native="twshow('3')">课程
        </router-link>
        <router-link class="menu-item" :class="{active:shows == 4}" to=""
                     @click.native="twshow('4')">院校云
        </router-link>
      </div>

      <!-- 用户个人页面 -->
      <div
          class="pull-right"
          v-if="type == 'student' || type == 'teacher' || type == 'admin'"
      >
        <div class="ulearning-tools">
          <a class="tool-item">
            <i class="el-icon-document iconfont"></i>
            <span>{{ $t("indexHeaders.order") }}</span>
          </a>
          <a class="tool-item" style="border-left: 1px dotted #969696">
            <i class="el-icon-tickets iconfont"></i>
            <span>{{ $t("indexHeaders.book") }}</span>
          </a>
          <a class="tool-item" style="border-left: 1px dotted #969696">
            <i class="el-icon-coin iconfont"></i>
            <span>{{ $t("indexHeaders.app") }}</span>
          </a>
          <a class="message-icon">
            <i class="el-icon-message iconfont"></i>
          </a>
        </div>
        <div class="btn-login">
          <div class="user-menu-component">
            <el-dropdown class="user-control" @command="handleCommand">
              <div class="el-dropdown-link user-info">
                <el-image class="avatar" :src="image" :fit="fit"></el-image>
                <span class="name">{{ name }}</span>
              </div>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="return" :disabled="this.$route.meta.isReturn">{{
                    $t("indexHeaders.return")
                  }}
                </el-dropdown-item>
                <el-dropdown-item command="returnMy"
                                  :disabled="!this.$route.meta.isReturn">
                  我的首页
                </el-dropdown-item>
                <el-dropdown-item command="manage">{{
                    $t("indexHeaders.manage")
                  }}
                </el-dropdown-item>
                <el-dropdown-item command="quit">{{
                    $t("indexHeaders.quit")
                  }}
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </div>
        </div>
        <div class="btn-i18n">
          <el-dropdown class="i18n-component" @command="changeLanguage">
            <div class="el-dropdown-link locale-control">
              <div class="locale-info">
                {{ $t("language") }}
              </div>
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="zh">简体中文</el-dropdown-item>
              <el-dropdown-item command="en">英文</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>


      <!-- 优学院主页面的div切换 -->
      <div class="pull-right" v-else>
        <div class="input-search-group">
          <input type="text" id="textInput"
                 class="text-input text-input-gray"
                 placeholder="搜索教材/课程/公开课"/>
          <i class="el-icon-search glyphicon glyphicon-search"></i>
        </div>
        <a class="down-app-a">
          <i class="el-icon-monitor iconfont icon-shouye-touping"
             style="vertical-align: middle;font-family: element-icons !important;"></i>
          <span class="name">投屏</span>
        </a>
        <a class="down-app-a">
          <i class="el-icon-question iconfont icon-shouye-touping"
             style="vertical-align: middle;font-family: element-icons !important;"></i>
          <span class="name">帮助</span>
        </a>
        <!-- <div v-text="$t('language')"></div> -->
        <div class="btn-login">
          <div class="user-menu-component">
            <button class="button button-red-solid button-login"
                    @click="login"

            >登录 / 注册
            </button>
          </div>
        </div>

        <div class="btn-i18n">
          <el-dropdown class="i18n-component" @command="changeLanguage">
            <div class="el-dropdown-link locale-control">
              <div class="locale-info" v-text="$t('language')"></div>
            </div>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="zh">简体中文</el-dropdown-item>
              <el-dropdown-item command="en">英文</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </div>
    </div>
  </el-header>
</template>

<script>
import "@/styles/css/el.css";
import "@/styles/css/button/el_button.css";
import {imageGet} from "@/apis/common";
import {collegeImageGet} from "@/apis/people";

export default {
  props: {},
  name: "indexHeader",
  data() {
    return {
      // 用户头像
      image: require("../../assets/common/null_avatar_image.jpg"),
      name: "", // 用户姓名
      type: "", // 用户身份
      fit: "cover",
      // 学校logo
      urlContentContainer: require("../../assets/common/null_college_image.jpg"),
      urlContentContainerElseIf: require("../../assets/common/admin.jpg"),
      urlContentContainerElse: require("../../assets/common/yxy_1.0.jpg"),
      // 标题active属性
      shows: 1,
    };
  },
  created() {

    this.init()
  },
  methods: {
    // 页面初始化
    async init() {


      let id = localStorage.getItem("id")
      let type = localStorage.getItem("type")
      // 获取用户身份信息
      this.type = type
      this.name = localStorage.getItem("name")
      // 获取用户头像地址
      imageGet(id).then((resp) => {
        if (resp?.data?.code == 1 && resp?.data?.data != null) {
          // 请求用户头像
          this.image = `/api/common/download?name=${resp?.data?.data}`
        }
      })
      let college = {
        id: id,
        type: type,
      }
      // 获取学校图片地址
      collegeImageGet(college).then((resp) =>{
        if(resp?.data?.code == 1){
          this.urlContentContainer = resp?.data?.data
        }
        //若未成功获取学校图片则不做处理，学校图片有默认值
      })


    },
    // 标题active设置
    twshow: function (i) {
      this.shows = i
    },

    // 下拉菜单选项触发事件
    handleCommand(command) {
      // TODO 返回学生、老师所在学校的首页
      if(command === 'return'){
        this.$router.push({
          path: '/'
        }).catch(r => console.log(r))
        console.log(command)
      }
      let type = localStorage.getItem("type")
      if(command === 'returnMy'){
        if(type === 'student'){
          this.$router.push({
            path: '/student'
          }).catch(r => console.log(r))
        }else if(type === 'teacher'){
          this.$router.push({
            path: '/teacher'
          }).catch(r => console.log(r))
        }else if(type === 'admin'){
          this.$router.push({
            path: '/admin'
          }).catch(r => console.log(r))
        }


      }
      if (command === 'manage') {
        this.$router.push({
          path: '/info/personal'
        }).catch(r => console.log(r))

      }

      // 退出登录
      if (command === 'quit') {
        if(this.$route.path == '/'){
          localStorage.clear()
          location.reload()
        }else{
          this.$router.push({
            name: 'home'
          })
          localStorage.clear()
        }

      }
    },

    // 切换语言
    changeLanguage(command) {
      this.$i18n.locale = command;
    },

    // 登录注册页面切换
    login() {
      this.$router.push({
        path: '/login'
      })
    }
  },
};
</script>

<style scoped>
body > .el-container {
  margin-bottom: 40px;
}

*,
:after,
:before {
  box-sizing: border-box;
}

body html {
  font-size: 14px;
}

a {
  text-decoration: none;
}

.index-page .content-container {
  width: 1170px;
}

.content-container {
  margin: auto;
}

.header-wrapper {
  margin: auto;
}

.login-wrapper {
  float: left !important;
  display: block;
  width: 290px;
  text-align: left;
}

.login-wrapper-image {
  display: inline-block;
  height: 40px;
  vertical-align: middle;
  overflow: clip;
}

.pull-right {
  float: right !important;
}

.btn-login {
  display: inline-block;
  vertical-align: middle;
}

.ulearning-tools {
  display: inline-block;
}

.ulearning-tools .tool-item {
  color: #444444;
  padding: 2px 12px;
  cursor: pointer;
}

.iconfont {
  margin-right: 6px;
  vertical-align: middle;
}

.index-header {
  position: fixed;
  top: 0;
  z-index: 11;
  width: 100%;
  line-height: 60px;
}

.index-page .index-header .btn-login {
  display: inline-block;
  vertical-align: middle;
}

.index-page .index-body {
  padding-top: 60px;
}

.admin-image {
  background-size: cover;
  background-repeat: no-repeat;
  background-position: 50%;
  position: relative;
  height: 120px;
}

.header-bg {
  position: absolute;

  transform: translateX(-50%);
  height: 100%;
}

.user-control .user-info {
  display: inline-block;
}

.user-menu-component {
  vertical-align: middle;
  display: inline-block;
  text-align: center;
  line-height: 1;
}

.user-menu-component .user-control {
  position: relative;
}

.user-menu-component .avatar {
  border-radius: 100%;
  width: 32px;
  height: 32px;
}

.avatar {
  vertical-align: middle;
}

.user-menu-component .user-info .name {
  display: inline-block;
}

.btn-i18n {
  margin-left: 8px;
  display: inline-block;
  vertical-align: middle;
}

.tool-item:hover {
  color: #ea5947;
}

.message-icon {
  color: #99a9bf;
}

.message-icon:hover {
  color: #ea5947;
}

.header-wrapper {
  position: relative;
  max-width: 1170px;
}

.head-image {
  margin: 20px 30px;
  padding: 3px;
  border-radius: 100%;
  border: 2px solid #fff;
  background-color: transparent;
  width: 80px;
  height: 80px;
  overflow: hidden;
}

.i18n-component .locale-control {
  position: relative;
  background-color: #cbcbd1;
  text-align: center;
  border-radius: 100%;
  width: 30px;
  height: 30px;
  line-height: 30px;
  color: #fff;
}

.i18n-component {
  line-height: 1;
  text-align: center;
}

.pull-left {
  float: left !important;
}

.head-image-avatar {
  width: 70px;
  height: 70px;
  border-radius: 100%;
}

.course-list-body {
  position: relative;
}

.index-footer {
  position: relative;
}

.pull-left-name {
  color: #fff;
  max-height: 36px;
  line-height: 18px;
  font-size: 18px;
  overflow: hidden;
}

.menu-item {
  margin-left: 28px;
  cursor: pointer;
  display: inline-block;
  color: #444;
  text-decoration: none;
}

.menu-item:hover {
  color: #ea5947;
}

.active {
  color: #ea5947;
}

.input-search-group {
  margin-right: 10px;
  border: 1px solid #e3e3e9;
  border-radius: 4px;
  background-color: #fff;
  display: inline-block;
  width: 210px;
  position: relative;
  height: 32px;
  line-height: 30px;
  font-size: 14px;
  vertical-align: middle;
}

.input-search-group:hover {
  border-color: #ea5947;
}

input {
  margin: 0;
}

.text-input {
  /*padding-left: 16px;*/
  /*padding-right: 34px;*/
  background-color: transparent;
  border: none;
  min-width: 200px;
  max-width: 400px;
  height: 100%;
  line-height: 30px;
  outline: none;
  overflow: hidden;
  text-overflow: ellipsis;
  box-sizing: border-box;
}

.glyphicon {
  right: 10px;
  margin: auto;
  cursor: pointer;
  position: absolute;
  top: 0;
  bottom: 0;
  height: 16px;
  font-size: 18px;
  color: #cbcbd1;
  display: inline-block;
  font-style: normal;
  font-weight: 400;
  line-height: 1;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

[class*=" el-icon-"], [class^=el-icon-] {
  vertical-align: middle;
}


a {
  background-color: transparent;
  text-decoration: none;
}

.down-app-a {
  margin-right: 10px;
  padding: 0;
  display: inline-block;
  color: #969696;
  font-size: 14px;
  line-height: 58px;
}

.down-app-a:hover {
  color: #ea5947;
  text-decoration: none;
}

.iconfont {
  font-size: 18px;
}

.down-app-a span {
  vertical-align: middle;
}
</style>